package stz.flash.option;

import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;
import net.rim.device.api.ui.Keypad;
import net.rim.device.api.util.Persistable;
import stz.flash.Info;
import stz.flash.option.strobe.StrobeTimeIntervalManager;
import stz.flash.string.OptionString;


public class PersistentData implements Persistable {

	private static PersistentData data;
	private static PersistentObject perObj;
	
	//App
	private int doublClickStartKey = Keypad.KEY_SPEAKERPHONE; 
	private String homeScreenHotkey = Info.ALPHABET_P;
	private Integer timerDuration = TimerDurationChoiceField.INTEGER_OF_3_MINUTE;
	private String selectedStartupMode = OptionString.OPTION_CHOICE_FIELD_MODE_LATEST; 
	private boolean vibration = true;
	private boolean backgroundExecute = true;

	//Flashlight
	private boolean startupLedState = false;
	private boolean startupBacklightState = false;
	private boolean startupFlashState = true; 
	
	//Strobe
	private int strobeToggleKey = (Info.isPearl3G()) ? Keypad.KEY_FORWARD 
			: Keypad.KEY_SPEAKERPHONE;

	private	Integer onTime = new Integer(30);
	private	Integer customOnTime = new Integer(30);//새로 정할것

	private Integer timeInterval = StrobeTimeIntervalManager.TIME_INTERVAL_INTEGER5;
	private Integer customTimeInterval = StrobeTimeIntervalManager.TIME_INTERVAL_INTEGER5;
	private int timeIntervalMainIndex;
	
	//other
	private int latestMode;
	private boolean vrFlashState = true;
	
	private PersistentData() {}
	
	public static PersistentData getPersistentDatabase(){
		if (data != null) 
			return data;
		loadPersistentData();
		return data;
	}

	private static void loadPersistentData() {
		perObj = PersistentStore.getPersistentObject(Info.OPTION_PERSISTENT_KEY);
		if (perObj.getContents() == null){
			perObj.setContents(new PersistentData());
			perObj.commit();
		}
		data = (PersistentData) perObj.getContents();
	}
	
	public static void commit(){
		perObj.commit();
	}
	
	//----------------------------------------App---------------------------------------
	public int getDoubleClickStartKey() {
		return doublClickStartKey;
	}
	public void setDoublClickStartKey(int key) {
		this.doublClickStartKey = key;
	}
	
	public String getHomeScreenHotkey() {
		return homeScreenHotkey;
	}
	public void setHomeScreenHotkey(String hotkey) {
		this.homeScreenHotkey = hotkey;
	}

	public Integer getTimerDuration() {
		return timerDuration;
	}
	public void setTimerDuration(Integer duration) {
		this.timerDuration = duration;
	}
	
	public boolean isVibration() {
		return vibration;
	}
	public void setVibration(boolean vibration) {
		this.vibration = vibration;
	}

	public boolean isBackgroundExecute() {
		return backgroundExecute;
	}
	public void setBackgroundExecute(boolean background) {
		this.backgroundExecute = background;
	}
	
	public String getSelectedStartupMode() {
		return selectedStartupMode;
	}
	public void setSelectedStartupMode(String selectedStartupMode) {
		this.selectedStartupMode = selectedStartupMode;
	}
	
	//-------------------------------------Flashlight-----------------------------------
	public boolean isStartupLedState() {
		return startupLedState;
	}
	public void setStartupLedState(boolean ledState) {
		this.startupLedState = ledState;
	}

	public boolean isStartupBacklightState() {
		return startupBacklightState;
	}
	public void setStartupBacklightState(boolean backlightState) {
		this.startupBacklightState = backlightState;
	}
	
	public boolean isStartupFlashState() {
		return startupFlashState;
	}
	public void setStartupFlashState(boolean flashState) {
		this.startupFlashState = flashState;
	}

	//--------------------------------------Strobe--------------------------------------
	public int getStrobeToggleKey() {
		return strobeToggleKey;
	}
	public void setStrobeToggleKey(int strobeToggleKey) {
		this.strobeToggleKey = strobeToggleKey;
	}
	
	public int getTimeIntervalMainIndex() {
		return timeIntervalMainIndex;
	}
	public void setTimeIntervalMainIndex(int index) {
		this.timeIntervalMainIndex = index;
	}
	
	/**
	 * strobe 가 한번 반짝이는 시간의 간격 관련 메서드
	 */
	public Integer getTimeInterval() {
		return timeInterval;
	}
	public void setTimeInterval(Integer time) {
		this.timeInterval = time;
	}

	public Integer getCustomTimeInterval() {
		return customTimeInterval;
	}
	public void setCustomTimeInterval(Integer time) {
		this.customTimeInterval = time;
	}
	
	/**
	 * strobe 의 켜져있는 시간 관련 메서드
	 */
	public Integer getOnTime() {
		return onTime;
	}
	public void setOnTime(Integer time) {
		this.onTime = time;
	}
	
	public Integer getCustomOnTime() {
		return customOnTime;
	}
	public void setCustomOnTime(Integer time) {
		this.customOnTime = time;
	}
	
	//other--------------------------------
	public int getLatestMode() {
		return latestMode;
	}
	public void setLatestMode(int mode) {
		latestMode = mode;
	}
	
	public boolean isVRFlashState() {
		return vrFlashState;
	}
	public void setVRFlashState(boolean vrFlashState) {
		this.vrFlashState = vrFlashState;
		perObj.commit();
	}
	
	
	//Test-------------------------------------------------------------
	boolean testActivity;
	boolean flashlightThreadState;
	boolean timerThreadState;


	public boolean isFlashlightThreadState() {
		return flashlightThreadState;
	}
	public void setFlashlightThreadState(boolean flashlightThreadState) {
		if (testActivity == false)
			return;
		this.flashlightThreadState = flashlightThreadState;
		perObj.commit();
	}

	public boolean isTimerThreadState() {
		return timerThreadState;
	}
	public void setTimerThreadState(boolean timerThreadState) {
		if (testActivity == false)
			return;
		this.timerThreadState = timerThreadState;
		perObj.commit();
	}

	public boolean isTestActivity() {
		return testActivity;
	}
	public void setTestActivity(boolean testActivity) {
		this.testActivity = testActivity;
	}
}